package oo

import (
	"elegantGo/chapter17/oo/article"
	"time"
)

type ArticleBuilder struct {
	id         int
	userID     int
	category   int
	title      string
	summary    string
	content    string
	readCount  int
	price      int
	saleCount  int
	createTime time.Time
	updateTime time.Time

	orders ArticleOrders
	user   User
}

func NewArticleBuilder() *ArticleBuilder {
	return &ArticleBuilder{}
}

func (builder *ArticleBuilder) Build() Article {
	var trait Article
	switch builder.category {
	case article.CategoryFree:
		trait = &FreeArticle{}
	case article.CategoryPaid:
		paidArticle := &PaidArticle{}
		if err := paidArticle.SetPrice(builder.price); err != nil {
			panic(err)
		}
		paidArticle.SetSaleCount(builder.saleCount)
		paidArticle.SetOrders(builder.orders)
		trait = paidArticle
	case article.CategoryVip:
		trait = &VipArticle{}
	default:
		panic("invalid category")
	}

	trait.SetID(builder.id)
	trait.SetCategory(builder.category)
	trait.SetTitle(builder.title)
	trait.SetSummary(builder.summary)
	trait.SetReadCount(builder.readCount)
	trait.SetContent(builder.content)
	trait.SetUser(builder.user)

	return trait
}

func (builder *ArticleBuilder) SetID(v int) *ArticleBuilder {
	builder.id = v
	return builder
}

func (builder *ArticleBuilder) SetUserID(v int) *ArticleBuilder {
	builder.userID = v
	return builder
}

func (builder *ArticleBuilder) SetCategory(v int) *ArticleBuilder {
	builder.category = v
	return builder
}

func (builder *ArticleBuilder) SetTitle(v string) *ArticleBuilder {
	builder.title = v
	return builder
}

func (builder *ArticleBuilder) SetSummary(v string) *ArticleBuilder {
	builder.summary = v
	return builder
}

func (builder *ArticleBuilder) SetContent(v string) *ArticleBuilder {
	builder.content = v
	return builder
}

func (builder *ArticleBuilder) SetReadCount(v int) *ArticleBuilder {
	builder.readCount = v
	return builder
}

func (builder *ArticleBuilder) SetPrice(v int) *ArticleBuilder {
	builder.price = v
	return builder
}

func (builder *ArticleBuilder) SetSaleCount(v int) *ArticleBuilder {
	builder.saleCount = v
	return builder
}

func (builder *ArticleBuilder) SetCreateTime(v time.Time) *ArticleBuilder {
	builder.createTime = v
	return builder
}

func (builder *ArticleBuilder) SetUpdateTime(v time.Time) *ArticleBuilder {
	builder.updateTime = v
	return builder
}

func (builder *ArticleBuilder) SetOrders(v ArticleOrders) *ArticleBuilder {
	builder.orders = v
	return builder
}

func (builder *ArticleBuilder) SetUser(v User) *ArticleBuilder {
	builder.user = v
	return builder
}
